package com.tjjingpan.notificationtest

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //获取NotificationManager实例
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //manager.cancel(1)
        //创建一个ID为normal通知渠道,Build.VERSION_CODES.O = 26 android 8.0
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel = NotificationChannel("normal","Normal",NotificationManager.IMPORTANCE_DEFAULT)
            val channel2 = NotificationChannel("important","Important",NotificationManager.IMPORTANCE_HIGH)
            manager.createNotificationChannel(channel)
            manager.createNotificationChannel(channel2)
        }
        sendNotice.setOnClickListener {
            val intent = Intent(this,NotificationActivity::class.java)
            val pi =PendingIntent.getActivity(this,0,intent,0)
            val notication = NotificationCompat.Builder(this,"important")
                .setContentTitle("This is content title")
//                .setContentText("Learn how to build notifications,send and sync data," +
//                        "and use voice actins .Get the official Android Ide and developer tools" +
//                        "to build apps for Android.")
                .setStyle(NotificationCompat.BigTextStyle().bigText("Learn how to" +
                        " build notifications,send and sync data,and use voice actins " +
                        ".Get the official Android Ide and developer tools " +
                        " to build apps for Android."))
                .setStyle(NotificationCompat.BigPictureStyle().bigPicture(
                    BitmapFactory.decodeResource(resources,R.drawable.big_image)))
                .setSmallIcon(R.drawable.small_icon)
                .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.large_icon))
                .setContentIntent(pi)
               .setAutoCancel(true)//下拉选择通知时取消系统状态栏上的通知图标
                .build()
            manager.notify(1,notication)
        }
    }
}